home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / iritsm3s.zip / CTRL-BRK.C < prev    next >
C/C++ Source or Header  |  1992-01-31  |  4KB  |  113 lines

  1. /*****************************************************************************
  2. * Module to trap ctrl-brk/hardware error and handle them gracefully.         *
  3. * Note the usage of GraphGen.c module is assumed.                 *
  4. *                                         *
  5. * Written by:  Gershon Elber                Ver 1.1, Mar. 1990   *
  6. *****************************************************************************/
  7.  
  8. #ifdef __MSDOS__
  9. #include <dos.h>
  10. #else
  11. #include <signal.h>
  12. #endif /* __MSDOS__ */
  13.  
  14. #include <stdio.h>
  15. #include "program.h"
  16. #include "ctrl-brk.h"
  17. #include "graphgen.h"
  18.  
  19. #ifdef __MSDOS__
  20. static void far *OldCtrlBrk; /* Save here old int 1bh to be able to restore. */
  21. #endif /* __MSDOS__ */
  22.  
  23. static int WasCtrlBrkSetUp = FALSE;  /* TRUE if SetUpCtrlBrk rtn was called. */
  24. int GlblWasCtrlBrk;
  25.  
  26. #ifdef __MSDOS__
  27.  
  28. /*****************************************************************************
  29. * Routine TrapCtrlBrk gain control if Control break was typed:             *
  30. *****************************************************************************/
  31. static void interrupt TrapCtrlBrk(void)
  32. {
  33.     GlblWasCtrlBrk = TRUE;
  34. }
  35.  
  36. #endif /* __MSDOS__ */
  37.  
  38. /*****************************************************************************
  39. * Routine TrapCtrlC gain control if Control C was typed (DOS level):         *
  40. *****************************************************************************/
  41. #ifdef __MSDOS__
  42. static int cdecl TrapCtrlC(void)
  43. #else
  44. static void TrapCtrlC(int Type)
  45. #endif /* __MSDOS__ */
  46. {
  47.     GlblWasCtrlBrk = TRUE;
  48.  
  49. #ifdef __MSDOS__
  50.     return -1;                        /* Resume execution. */
  51. #else
  52.     printf("\n*** Break ***\n");
  53.     signal(SIGINT, TrapCtrlC);
  54.     signal(SIGQUIT, TrapCtrlC);
  55. #endif /* __MSDOS__ */
  56. }
  57.  
  58. /*****************************************************************************
  59. * Routine SetUpCtrlBrk must be called once by main program:             *
  60. *****************************************************************************/
  61. void SetUpCtrlBrk(void)
  62. {
  63.     if (WasCtrlBrkSetUp) return;          /* No need to to it twice! */
  64.  
  65. #ifdef __MSDOS__
  66.     OldCtrlBrk = getvect(BIOS_CTRL_BRK);
  67.     setvect(BIOS_CTRL_BRK, TrapCtrlBrk);
  68.  
  69.     ctrlbrk(TrapCtrlC);
  70. #else
  71.     signal(SIGINT, TrapCtrlC);
  72.     signal(SIGQUIT, TrapCtrlC);
  73. #endif /* __MSDOS__ */
  74.  
  75.     WasCtrlBrkSetUp = TRUE;
  76. }
  77.  
  78. /*****************************************************************************
  79. * Routine RestoreCtrlBrk must be called before the program quit:         *
  80. *****************************************************************************/
  81. void RestoreCtrlBrk(void)
  82. {
  83. #ifdef __MSDOS__
  84.     if (WasCtrlBrkSetUp)
  85.     setvect(BIOS_CTRL_BRK, (void interrupt (*)()) OldCtrlBrk);
  86. #endif /* __MSDOS__ */
  87.  
  88.     WasCtrlBrkSetUp = FALSE;
  89. }
  90.  
  91. #ifdef __MSDOS__
  92.  
  93. /*****************************************************************************
  94. * Routine TrapHardWareError gain control if hardware fails - int 24.         *
  95. *****************************************************************************/
  96. static int cdecl TrapHardWareError(int errval, int ax, int bp, int si)
  97. {
  98.     return IGNORE;
  99. }
  100.  
  101. #endif /* __MSDOS__ */
  102.  
  103. /*****************************************************************************
  104. * Routine SetUpHardError must be called once by main program:             *
  105. * harderr set interrupt vector 0x024 to point to TrapHardWareError.         *
  106. *****************************************************************************/ 
  107. void SetUpHardErr(void)
  108. {
  109. #ifdef __MSDOS__
  110.     harderr(TrapHardWareError);
  111. #endif /* __MSDOS__ */
  112. }
  113.